package b3.model
{
	import b3.config.*;
	import b3.net.*;
	import b3.scene.home.*;
	import b3.util.*;
	import b3.widget.basic.*;
	
	import flash.events.Event;
	import flash.external.*;
	import flash.net.*;
	import flash.utils.*;

	public class PackageModel extends Model
	{
		public var ownerId:int;
		
		public var objectList:Object;
		
		public function PackageModel()
		{
			super();
			objectList = new Object();
			ownerId = -1;
		}
		
		public function getObjectNum(objectIndex:int):int 
		{
			if (objectIndex == ObjConfig.POPULATION)
			{
				var objectNum:int = 0;
				for (var i:int = 0; i < 7; i++)
				{
					objectNum += (objectList[ObjConfig.SECTION_POPULATION + i] == null ? 0 : objectList[ObjConfig.SECTION_POPULATION + i].objectNum);
				}
				return objectNum;
			}
			else
			{
				return objectList[objectIndex] == null ? 0 : objectList[objectIndex].objectNum;
			} 
		}
		
		public function loadObjects(firstLoad:Boolean = false, force:Boolean = false):void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.LOAD_OBJECTS, {ownerId:ownerId, firstLoad:firstLoad});
			serverRequest.setHandleFunc(handleLoadObjects);
			serverRequest.post(force);
		}
		
		public function handleLoadObjects(data:Object):void
		{
			var firstLoad:Boolean = data['firstLoad'];
			var objects:Object = data['objects'];
			objectList = new Object();
			for (var s:String in objects)
			{
				var obj:Obj = new Obj();
				obj.loadbyObject(objects[s]);
				objectList[s] = obj;
			}
			var event:ModelEvent = new ModelEvent(ModelEvent.REFRESH);
			event.data = new Object();
			event.data.firstLoad = firstLoad;
			dispatchEvent(event);
		}
		
		public function objectBalance(obj:Obj, force:Boolean = false):void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.OBJECT_BALANCE, {obj:obj, ownerId:ownerId});
			serverRequest.setHandleFunc(handleObjectBanlance);
			serverRequest.post(force);
		}
		
		private function handleObjectBanlance(data:Object):void
		{
			handleLoadObjects(data['package']);
			var event:ModelEvent = new ModelEvent(ModelEvent.OBJECT_BALANCE);
			event.data = data;
			dispatchEvent(event);
		}
		
		public function purchase(objectIndex:int, objectNum:int, force:Boolean = false):void
		{
			var objectConfig:ObjConfig = Config.objConfigs[objectIndex];
			var priceObj:Obj = new Obj();
			var price:int;
			if (objectConfig.priceGoldIngot > 0)
			{
				priceObj.objectIndex = ObjConfig.GOLDINGOT;
				price = objectConfig.priceGoldIngot; 
			}
			else if (objectConfig.priceGoldCoin > 0)
			{
				priceObj.objectIndex = ObjConfig.GOLDCOIN;
				price = objectConfig.priceGoldCoin;
			}
			else if (objectConfig.priceLight > 0)
			{
				priceObj.objectIndex = ObjConfig.LIGHTCOIN;
				price = objectConfig.priceLight;
			}
			else
			{
				priceObj.objectIndex = ObjConfig.DARKCOIN;
				price = objectConfig.priceDark;
			}
			if (objectConfig.priceObjectIndex > 0)
				priceObj.objectIndex = objectConfig.priceObjectIndex;
			priceObj.objectNum = -objectNum * int(objectConfig.discount > 0 ? price * objectConfig.discount / 100 : price);
			var purchaseObj:Obj = new Obj();
			purchaseObj.objectIndex = objectIndex;
			purchaseObj.objectNum = objectNum;
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.PURCHASE, {priceObj: priceObj, purchaseObj:purchaseObj});
			serverRequest.setHandleFunc(handlePurchase);
			serverRequest.post(force);
		}
		
		private function handlePurchase(data:Object):void
		{
			handleLoadObjects(data['package']);
			var modelEvent:ModelEvent = new ModelEvent(ModelEvent.PURCHASE_COMPLETE);
			modelEvent.data = data;
			dispatchEvent(modelEvent);
		}
		
		//Q点直购
		public function buyGoods(payitem:String, goodsmeta:String, goodsurl:String):void
		{
			var pm:PlayerManager = PlayerManager.getInstance();
			//准备参数
			var variables:URLVariables = new URLVariables();
			variables.openid = pm.openid;
			variables.openkey = pm.openkey;
			variables.pf = pm.pf;
			variables.pfkey = pm.pfkey;
			variables.payitem = payitem;
			variables.goodsmeta = goodsmeta;
			variables.goodsurl = goodsurl;
			//发送请求
			var request:URLRequest = new URLRequest(Profile.URL_BuyGoods);
			request.method = URLRequestMethod.POST;
			request.data = variables;
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, handleBuyGoods);
			loader.load(request);
		}
		
		private function handleBuyGoods(event:Event):void
		{
			DebugPane.getInstance().addInfo("handleBuyGoods" + event.target.data);
			var url_params:String = event.target.data;
			ExternalInterface.call("tfBuyItem", url_params);
		}
		
	}
}